import moment from 'moment';

// 2020-08-15 -> 2020年8月15日
export const dateToZhDate = (date: string): string => {
  return moment(date).format('YYYY年MM月DD日');
};

// 2020-08-15 -> 2020/08/15
export const dateToSlashDate = (date: string): string => {
  const dateArray = date.split('-');
  if (dateArray.length === 3) {
    const [year, month, day] = dateArray;
    return `${year}/${month}/${day}`;
  }
  return date;
};

// 1800s -> 30:00
export const secondToMinute = (seconds: number): string => {
  const minute = Math.floor(seconds / 60);
  const second = seconds % 60;

  return `${minute < 10 ? `0${minute}` : minute}:${
    second < 10 ? `0${second}` : second
  }`;
};

// 2020-04-03 10:00:00 -> timestamp
export const dateToTimestamp = (date: string, needMs = false): number => {
  const transDate = date.replace(/-/g, '/');
  const timestamp = new Date(transDate).getTime();

  return needMs ? timestamp : timestamp / 1000;
};

// 2020-08-15 00:00:00 -> 8月15日
export const monthToZhMonth = (date: string): string => {
  const dateArray = date.split(' ')[0].split('-');

  if (dateArray.length === 3) {
    const [year, month, day] = dateArray;
    return `${month.replace(/\b(0+)/gi, '')}月${day.replace(/\b(0+)/gi, '')}日`;
  }
  return date;
};

// 2020-08-15 00:00:00 -> 2020.08.15
export const dateToLineDate = (date: string): string => {
  const dateArray = date.split(' ');

  return dateArray[0].replace(/-/g, '.');
};

// 2020-08-15 00:00:00 -> 08.15
export const dateToLineMonth = (date: string): string => {
  const dateArray = date.split(' ')[0].split('-');

  if (dateArray.length === 3) {
    const [year, month, day] = dateArray;
    return `${month}.${day}`;
  }
  return date;
};

// 2020-08-15 -> 2020.08.15
export const dateToDotDate = (date: string): string => {
  return date.replace(/-/g, '.');
};

// 2020-08-15 -> 2020.08.15
export const dateToDotMonth = (date: string): string => {
  const dateArray = date.split('-');
  if (dateArray.length === 3) {
    const [year, month, day] = dateArray;
    return `${month}.${day}`;
  }
  return date;
};

// 2020-08-15 10:23:23 -> 2020.08.15 10:23
export const dateToDotMinute = (date: string): string => {
  return moment(date).format('YYYY.MM.DD hh:mm');
};

// 1800s -> 30:00
export const secondToZhMinute = (seconds: number): string => {
  const minute = Math.floor(seconds / 60);
  const second = seconds % 60;

  return `${minute < 10 ? `0${minute}` : minute}分${
    second < 10 ? `0${second}` : second
  }秒`;
};
